'use strict';
const http = require("http"), https = require("https");

module.exports = (request, response, rfun)=>{
    const httpc = require("../config/http"), pubfun = require("./pubfun");
    let op = {"host": "", "port": 80, "path": request.url };
    if(request.GET.pathname in httpc[request.headers.host].proxyList) op.host = httpc[request.headers.host].proxyList[request.GET.pathname];
    else op.host = httpc[request.headers.host].proxy.host;
    op.method = request.method;
    op.headers = request.headers;
    delete op.headers.referer;
    delete op.headers.host;
    console.log("headers", op.headers);
    pubfun.getPost(request, (buf)=>{
        let req = http.request(op, (res)=>{
            console.log("res.headers", res.headers);
            if(res.headers['set-cookie']){
                for(let i=0;i<res.headers['set-cookie'].length;i++){
                    res.headers['set-cookie'][i] = res.headers['set-cookie'][i].replace(/DOMAIN=qq.com;/g, "");
                    res.headers['set-cookie'][i] = res.headers['set-cookie'][i].replace(/DOMAIN=qb.qq.com;/g, "");
                }
                response.setHeader("Set-Cookie", res.headers['set-cookie']);
            } 
            res.pipe(response);
        });
        console.log("buf", buf.length, buf.toString());
        req.write(buf);
        req.end();
    });
}